﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using D3DSS.Model;
using System.ComponentModel.DataAnnotations;

namespace D3DSS.Repository
{
    /// <summary>
    /// 模型数据上下文
    /// </summary>
    public class ModelDbContext : DbContext
    {
        public ModelDbContext(string connectionString)
            : base(connectionString)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder = AccountContext(modelBuilder);
            modelBuilder = DropContext(modelBuilder);
            modelBuilder = StatisticsOrderContext(modelBuilder);
            modelBuilder = UpdateContext(modelBuilder);

            base.OnModelCreating(modelBuilder);
        }

        /// <summary>
        /// 更新数据上下文
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <returns></returns>
        protected DbModelBuilder UpdateContext(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UpdateInfoModel>()
                .ToTable("UpdateInfo");

            modelBuilder.Entity<UpdateInfoModel>()
                .HasKey(u => u.ID)
                .Property(u => u.ID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            modelBuilder.Entity<UpdateInfoModel>()
               .Property(u => u.Version)
               .IsRequired()
               .HasMaxLength(16);

            modelBuilder.Entity<UpdateInfoModel>()
               .Property(u => u.UpdateNote)
               .IsRequired()
               .HasMaxLength(2048);

            modelBuilder.Entity<UpdateInfoModel>()
               .Property(u => u.DownloadUrl)
               .IsRequired()
               .HasMaxLength(128);

            modelBuilder.Entity<AccountModel>()
               .Property(u => u.CreateTime)
               .IsRequired();

            return modelBuilder;
        }

        /// <summary>
        /// 账户数据上下文
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <returns></returns>
        protected DbModelBuilder AccountContext(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AccountModel>()
                .ToTable("Account");

            modelBuilder.Entity<AccountModel>()
                .HasKey(a => a.ID)
                .Property(a => a.ID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            modelBuilder.Entity<AccountModel>()
                .Property(a => a.Account)
                .IsRequired()
                .HasMaxLength(64);

            modelBuilder.Entity<AccountModel>()
                .Property(a => a.Password)
                .IsRequired()
                .HasMaxLength(32);

            modelBuilder.Entity<AccountModel>()
                .Property(a => a.CreateTime)
                .IsRequired();

            return modelBuilder;
        }

        /// <summary>
        /// 掉落统计项数据上下文
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <returns></returns>
        protected DbModelBuilder DropContext(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DropModel>()
                .ToTable("Drop");

            modelBuilder.Entity<DropModel>()
                .HasKey(a => a.ID)
                .Property(a => a.ID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            modelBuilder.Entity<DropModel>()
                .Property(a => a.CreateTime)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.IsExtend)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.LegendDrop)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.Level61RaraDrop)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.Level62RaraDrop)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.Level63RaraDrop)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.MagicDrop)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.MF)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.Nephalem)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.RaraDrop)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.RoomIP)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.RoomParentIP )
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.StatisticsOrderID)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.TotalDrop)
                .IsRequired();

            modelBuilder.Entity<DropModel>()
                .Property(a => a.WhiteDrop)
                .IsRequired();

            return modelBuilder;
        }

        /// <summary>
        /// 掉落统计单数据上下文
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <returns></returns>
        protected DbModelBuilder StatisticsOrderContext(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<StatisticsOrderModel>()
               .ToTable("StatisticsOrder");

            modelBuilder.Entity<StatisticsOrderModel>()
                .HasKey(a => a.ID)
                .Property(a => a.ID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            modelBuilder.Entity<StatisticsOrderModel>()
                .Property(a => a.Act)
                .IsRequired();

            modelBuilder.Entity<StatisticsOrderModel>()
                .Property(a => a.CreateTime)
                .IsRequired();

            modelBuilder.Entity<StatisticsOrderModel>()
                .Property(a => a.GameRole)
                .IsRequired();

            modelBuilder.Entity<StatisticsOrderModel>()
                .Property(a => a.GameServer)
                .IsRequired();

            modelBuilder.Entity<StatisticsOrderModel>()
                .Property(a => a.MF)
                .IsRequired();

            modelBuilder.Entity<StatisticsOrderModel>()
                .Property(a => a.RoomIP)
                .IsRequired();

            modelBuilder.Entity<StatisticsOrderModel>()
                .Property(a => a.RoomParentIP)
                .IsRequired();

            modelBuilder.Entity<StatisticsOrderModel>()
                .Property(a => a.UserID)
                .IsRequired();

            return modelBuilder;
        }
    }
}
